package
{
	import Interface.CGameStateUI_Result;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	public class CGameState_Result extends CGameState
	{
		private var m_GameStateUI :CGameStateUI_Result;
		
		public function CGameState_Result(refGame :CGame)
		{
			super(refGame);
			
			cMovieClip = new CMC_GameState_Result();
			m_GameStateUI = new CGameStateUI_Result(cMovieClip);
			m_GameStateUI.AddButtonClickHandler(OnClick_Next, OnClick_GoToTitle);
		}
		override public function EnterState() :void
		{
			m_GameStateUI.AddButtonClickHandler(OnClick_Next, OnClick_GoToTitle);
			m_GameStateUI.BuildUI();
			
			cMovieClip.stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
		}
		override public function LeaveState() :void
		{
			cMovieClip.stage.removeEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
			
			m_GameStateUI.RemoveButtonClickHandler(OnClick_Next, OnClick_GoToTitle);
		}
		private function ProcessNextButton() :void
		{
			if (CGameStageManager.instance.nCurrentStageNumber == CGameStageManager.instance.nStageCount-1)
			{
				CGameStageManager.instance.Clear();
				m_refGame.EnterState(CGameState.GAME_STATE_ENDING);
				
				CSoundManager.instance.PlaySound(CSoundManager.SOUND_5_BYEBYE);
			}
			else
			{
				CGameScoreManager.instance.ClearWithoutStaticGameData();
				CGameStageManager.instance.NextStage();
				m_refGame.EnterState(CGameState.GAME_STATE_PLAY);
				
				CSoundManager.instance.PlaySound(CSoundManager.SOUND_4_START);
			}
		}
		private function ProcessGoToTitle() :void
		{
			CGameScoreManager.instance.Clear();
			CGameStageManager.instance.Clear();
			m_refGame.EnterState(CGameState.GAME_STATE_TITLE);
			
			CSoundManager.instance.PlaySound(CSoundManager.SOUND_5_BYEBYE);
			CSoundManager.instance.StopSound(CSoundManager.SOUND_2_GAME);
			CSoundManager.instance.PlaySound(CSoundManager.SOUND_1_TITLE, int.MAX_VALUE);
		}
		
		private function OnClick_GoToTitle(event :Event) :void
		{
			ProcessGoToTitle();
		}
		private function OnClick_Next(event :Event) :void
		{
			ProcessNextButton();
		}
		private function OnKeyDown(event :KeyboardEvent) :void
		{
			if (event.keyCode == Keyboard.ENTER)
			{
				if (CGameScoreManager.instance.bIsSuccess == true)
				{
					ProcessNextButton();
				}
				else
				{
					ProcessGoToTitle();
				}
			}
		}
		
	}
}